package com.moyu.control;

import com.moyu.main.Main;
import com.moyu.activity.PanelGame;
import com.moyu.frame.myFrameGame;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class PlayerControl extends KeyAdapter {
    public PanelGame panelGame;

    public PlayerControl(PanelGame panelGame) {
        this.panelGame = panelGame;
    }

    //键盘按下
    @Override
    public void keyPressed(KeyEvent e) {
        if (panelGame.getStartGame()) {
            switch (e.getKeyCode()) {
                //上
                case KeyEvent.VK_UP:
                    panelGame.toup();
                    panelGame.repaint();
                    break;
                //左
                case KeyEvent.VK_LEFT:
                    panelGame.toleft();
                    panelGame.repaint();
                    break;
                //下
                case KeyEvent.VK_DOWN:
                    panelGame.todown();
                    panelGame.repaint();
                    break;
                //右
                case KeyEvent.VK_RIGHT:
                    panelGame.toright();
                    panelGame.repaint();
                    break;
                //加速
                case KeyEvent.VK_ADD:
                case 46:
                    panelGame.TimeAdd();
                    break;
                //减速
                case KeyEvent.VK_SUBTRACT:
                case 44:
                    panelGame.TimeSub();
                    break;
            }
        }
        //按回车开始游戏或重新开始游戏
        if (e.getKeyCode() == 10) {
            if (!panelGame.getStartGame()) {
                if (panelGame.getAlreadyloose()) {
                    Main.myFrameGame.dispose();
                    Main.myFrameGame = new myFrameGame(true);
                }
                panelGame.setStartGame();
                panelGame.GameThread(panelGame);
            } else {
                panelGame.setPauseGame();
            }
        }
    }
}
